package com.liuym.common.db;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;


@Configuration
@PropertySource(value = "classpath:application-common.properties", ignoreResourceNotFound = true)
public class BaseDataSource extends BasicDataSource {

    @Value("${db.datasource.base.url}")
    private String baseUrl;

    @Primary
    @Bean(name = "dataSourceGrade", destroyMethod = "close")
    public HikariDataSource dataSourceGrade() {

        return getDataSource(baseUrl);
    }

    @Primary
    @Bean(name = "transactionManagerGrade")
    public DataSourceTransactionManager transactionManagerGrade() {
        return new DataSourceTransactionManager(dataSourceGrade());
    }

    @Primary
    @Bean(name = "sqlSessionFactoryGrade")
    public SqlSessionFactory sqlSessionFactoryBase(@Qualifier("dataSourceGrade") HikariDataSource dataSource) throws Exception {
        return getSqlSessionFactory(dataSource);
    }

   /* @Primary
    @Bean(name = "sqlSessionBase")
    public EcIbatisSqlSessionTemplate sqlSessionBase(@Qualifier("sqlSessionFactoryBase") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new EcIbatisSqlSessionTemplate(sqlSessionFactory);
    }*/

}
